package fileManagers;

import java.io.File;

import utils.ArrayUtilities;
import android.os.Environment;

public class FileListGetter {

	private String directory;
	private boolean showHidden;
	
	public FileListGetter() {
		directory = Environment.getExternalStorageDirectory().getAbsolutePath();
		showHidden = false;
	}

	/**
	 * 
	 * @return ritorna un vettore di stringhe che contiene l'elenco dei file
	 *         contenuti nella directory.
	 */
	public String[] getList() {
		
		File folder = new File(directory);
		String[] files = folder.list();

		String[] list1 = FileFilter.filterList(files, directory, Filter.folder);
		String[] list2 = FileFilter.filterList(files, directory, Filter.image);

		files = ArrayUtilities.mergeList(list1, list2);

		if (folder.isDirectory()) {
			if (!showHidden) {
				files = FileFilter.filterList(files, directory, Filter.hidden);
			}
			if (files.length == 0) {
				return new String[] { "No files found" };
			}
			return files;
		} else {
			return null;
		}
	}

	/**
	 * 
	 * @return directory
	 */
	public String getDirectory() {
		return directory;
	}

	/**
	 * 
	 * Setta la directory
	 * 
	 * @param directory
	 */
	public void setDirectory(String directory) {
		this.directory = directory;
		while (this.directory.endsWith("/")) {
			this.directory = this.directory.substring(0,
					this.directory.length() - 1);
		}
	}

	/**
	 * 
	 * Modifica impostazione di visualizzazione dei file
	 * 
	 * @param showHidden
	 */
	public void setShowHidden(boolean showHidden) {
		this.showHidden = showHidden;
	}

	/**
	 * Setta la directory come directory padre
	 */
	public void goToParent() {
		directory = new File(directory).getParent();
	}

	/**
	 * 
	 * Setta la directory a quella figlia
	 * 
	 * @param subdirectory
	 * @see setDirectory
	 */
	public void goToSubDirectory(String subdirectory) {
		setDirectory(directory + "/" + subdirectory);
	}

	/**
	 * 
	 * @return true se la directory � quella di External Storage
	 */
	public boolean isExternalStorageDirectory() {
		return Environment.getExternalStorageDirectory().toString().length() == directory
				.length();
	}
	
	public String[] getPathList(){
		String[] list=getList();
		for (int i = 0; i < list.length; i++) {
			list[i]=FileUtilities.nameToPath(directory, list[i]);
		}
		return list;
	}
	
}
